from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.
class SysUser(AbstractUser):
    # first_name, last_name, email, username, password
    tel = models.CharField(verbose_name='手机号', max_length=11)
    nickname = models.CharField(max_length=50, verbose_name='昵称')
    role_name = models.CharField(max_length=20, default='普通用户',
                                 verbose_name='角色名称')

    head = models.CharField(verbose_name='用户头像',
                            max_length=200,
                            default='sysuser/default.jpg')
    source = models.CharField(verbose_name='来源',
                              default='平台', max_length=50)

    @property
    def role_code(self):
        return Role.objects.filter(name=self.role_name).get()

    class Meta:
        db_table = 'tb_sys_user'


class Role(models.Model):
    name = models.CharField(max_length=20)
    code = models.CharField(max_length=20)

    class Meta:
        db_table = 'tb_sys_role'


class Menu(models.Model):
    name = models.CharField(max_length=20)
    icon = models.CharField(max_length=50,
                            default='fa-caret-right')
    path = models.CharField(max_length=50, default='#')
    parent = models.ForeignKey('self',
                               on_delete=models.CASCADE,
                               related_name='submenus')

    class Meta:
        db_table = 'sys_menu'

class UserMenu(models.Model):
    user = models.ForeignKey(SysUser,
                             related_name='menus',
                             on_delete=models.CASCADE)
    menu = models.ForeignKey(Menu, on_delete=models.CASCADE)
    class Meta:
        db_table = 'sys_user_menu'


class Category(models.Model):
    name = models.CharField(max_length=30)
    level = models.IntegerField(default=1)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, related_name='children')

    class Meta:
        db_table = 'tb_category'

class Product(models.Model):
    name = models.CharField(max_length=50)
    cover = models.CharField(verbose_name='封面', max_length=100)
    price = models.FloatField(verbose_name='单价', default=0.0)
    summery = models.TextField(verbose_name='产品概要')

    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')

    class Meta:
        db_table = 'tb_product'


class ProductDetail(models.Model):  # 产品详情
    pass


class ProductImage(models.Model):  # 产品展示图片
    product = models.ForeignKey(Product,
                                on_delete=models.CASCADE,
                                related_name='images')

    path = models.CharField(max_length=50, verbose_name='图片路径')
    title = models.CharField(max_length=50, default='', blank=True, null=True)

    class Meta:
        db_table = 'tb_product_images'
